﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;
using Common;

namespace BestBuy.UserViews
{
    public partial class Payment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user != null)
            {
                LoadPaymentList(user);
            }
        }

        private void LoadPaymentList(User user)
        {
            gvPayment.DataSource = AuctionTransactionDAL.GetAuctionTransactionByUserID(user.ID);
            gvPayment.DataBind();
        }
        protected void btnPaymet_Click(object sender, EventArgs e)
        {
            Button senderButton = sender as Button;
            int auctionID = Int32.Parse(senderButton.Attributes["ObjectId"]);
            AuctionTransactionDAL.BuyerPaymentAuctionTransaction(auctionID, (int)AuctionTransactionStatus.Success);
            MessagePanel.Show("Payment success.", UserControls.MessagePanel.MessageType.Success);
        }

        protected void gvPayment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int aucID = (int)DataBinder.Eval(e.Row.DataItem, "AuctionID");
                Auction auc = AuctionDAL.GetAuctionByID(aucID);
                if (auc != null)
                {
                    HyperLink link = (HyperLink)e.Row.FindControl("AuctionLink");
                    if (link != null)
                    {
                        link.Text = auc.Title;
                        link.NavigateUrl = string.Format("~/UserViews/SellerBid.aspx?AuctionID={0}", auc.ID);
                    }
                }
            }
        }
    }
}